package client.ui.states;

/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * LoginState.java
 *
 * Steven Louie Apr. 8, 2012
 */
import client.ui.StateManager;
import client.ui.controllers.LoginScreen;
import client.ui.controllers.RegisterScreen;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
import de.lessvoid.nifty.controls.label.builder.LabelBuilder;
import de.lessvoid.nifty.controls.textfield.builder.TextFieldBuilder;
import de.lessvoid.nifty.loaderv2.NiftyLoader;
import de.lessvoid.nifty.loaderv2.types.NiftyType;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.slick2d.NiftyBasicGameState;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.state.StateBasedGame;

public class LoginState extends NiftyBasicGameState {

    @Override
    protected void prepareNifty(Nifty nifty, StateBasedGame sbg) {

        NiftyType niftyType = new NiftyType();
        try {
            NiftyLoader niftyLoader = nifty.getLoader();
            niftyLoader.loadStyleFile("nifty-styles.nxs", "nifty-default-styles.xml", niftyType, nifty);
            niftyLoader.loadControlFile("nifty-controls.nxs", "nifty-default-controls.xml", niftyType);
            niftyType.create(nifty, nifty.getTimeProvider());
        } catch (Exception e) {
            Logger.getLogger(LoginState.class.getName()).log(Level.SEVERE, null, e);
        }

        Screen startScreen = new ScreenBuilder("start") {

            {
                controller(new LoginScreen());
                layer(new LayerBuilder("bg") {

                    {
                        backgroundImage("/res/images/bg/bg.png");
                    }
                });
                layer(new LayerBuilder("fg") {

                    {
                        childLayoutCenter();
                        backgroundColor("#00000000");
                        panel(new PanelBuilder("container") {

                            {
                                style("nifty-panel");
                                childLayoutAbsolute();
                                width("40%");
                                height("30%");
                                control(new LabelBuilder() {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        text("Username");
                                        x("10%");
                                        y("10%");
                                    }
                                });
                                control(new TextFieldBuilder("username") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        maxLength(20);
                                        x("40%");
                                        y("10%");
                                        width("50%");
                                    }
                                });
                                control(new LabelBuilder() {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        text("Password");
                                        x("10%");
                                        y("30%");
                                    }
                                });
                                control(new TextFieldBuilder("password") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        passwordChar('*');
                                        maxLength(20);
                                        x("40%");
                                        y("30%");
                                        width("50%");
                                    }
                                });
                                control(new LabelBuilder("info") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        text("");
                                        color("#FF0000");
                                        x("10%");
                                        y("50%");
                                        width("85%");
                                    }
                                });
                                control(new ButtonBuilder("login", "Login") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        x("13%");
                                        y("70%");
                                        visibleToMouse(true);
                                        interactOnClick("login()");
                                    }
                                });
                                control(new ButtonBuilder("register", "Register") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        x("53%");
                                        y("70%");
                                        visibleToMouse(true);
                                        interactOnClick("gotoRegister()");
                                    }
                                });
                            }
                        });
                    }
                });
            }
        }.build(nifty);

        Screen registerScreen = new ScreenBuilder("register") {

            {
                controller(new RegisterScreen());
                layer(new LayerBuilder("bg") {

                    {
                        childLayoutCenter();
                        backgroundImage("/res/images/bg/bg.png");
                    }
                });
                layer(new LayerBuilder("fg") {

                    {
                        childLayoutCenter();
                        backgroundColor("#00000000");
                        panel(new PanelBuilder("container") {

                            {
                                childLayoutAbsolute();
                                style("nifty-panel");
                                width("40%");
                                height("70%");
                                control(new LabelBuilder() {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        text("First Name");
                                        x("5%");
                                        y("5%");
                                    }
                                });
                                control(new TextFieldBuilder("firstname") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        maxLength(20);
                                        x("40%");
                                        y("5%");
                                        width("50%");
                                    }
                                });
                                control(new LabelBuilder() {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        text("Last Name");
                                        x("5%");
                                        y("15%");
                                    }
                                });
                                control(new TextFieldBuilder("lastname") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        maxLength(20);
                                        x("40%");
                                        y("15%");
                                        width("50%");
                                    }
                                });
                                control(new LabelBuilder("info") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        text("");
                                        color("#FF0000");
                                        x("5%");
                                        y("25%");
                                        width("85%");
                                    }
                                });
                                control(new LabelBuilder() {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        text("Username");
                                        x("5%");
                                        y("35%");
                                    }
                                });
                                control(new TextFieldBuilder("username") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        maxLength(20);
                                        x("40%");
                                        y("35%");
                                        width("50%");
                                    }
                                });
                                control(new LabelBuilder() {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        text("Password");
                                        x("5%");
                                        y("45%");
                                    }
                                });
                                control(new TextFieldBuilder("password") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        maxLength(20);
                                        passwordChar('*');
                                        x("40%");
                                        y("45%");
                                        width("50%");
                                    }
                                });
                                control(new LabelBuilder() {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        text("Retype PW");
                                        x("5%");
                                        y("55%");
                                    }
                                });
                                control(new TextFieldBuilder("passwordC") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        maxLength(20);
                                        passwordChar('*');
                                        x("40%");
                                        y("55%");
                                        width("50%");
                                    }
                                });
                                control(new LabelBuilder("info2") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        text("");
                                        color("#FF0000");
                                        x("5%");
                                        y("65%");
                                        width("85%");
                                    }
                                });
                                control(new LabelBuilder() {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        text("E-mail");
                                        x("5%");
                                        y("75%");
                                    }
                                });
                                control(new TextFieldBuilder("email") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        x("40%");
                                        y("75%");
                                        width("50%");
                                    }
                                });
                                control(new ButtonBuilder("register", "Register") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        x("13%");
                                        y("90%");
                                        visibleToMouse(true);
                                        interactOnClick("register()");
                                    }
                                });
                                control(new ButtonBuilder("cancel", "Cancel") {

                                    {
                                        font("res/fonts/REFSAN.ttf");
                                        x("53%");
                                        y("90%");
                                        visibleToMouse(true);
                                        interactOnClick("gotoLogin()");
                                    }
                                });
                            }
                        });
                    }
                });
            }
        }.build(nifty);

        nifty.addScreen(startScreen.getScreenId(), startScreen);
        nifty.addScreen(registerScreen.getScreenId(), registerScreen);
    }

    @Override
    public int getID() {
        return StateManager.LOGIN;
    }
}
